var co = require('co');
var moment = require("moment");
var fs = require('fs');
var formidable = require('formidable');
var seqModule = require('../../models/index');
var db = new seqModule();
var util = require('../../common/util');
var meetOrderController = {
    addMeetOrder: function (req, res) {
        co(function () {
           req.body.id = util.getUUID();
           req.body.createTime = util.getNowTime();
           db.meetOrderModule.count({where:{'meetScheduleId':req.body.meetScheduleId,'openId':req.body.openId}}).then(function(count){
            if(count == 0){
                db.meetOrderModule.create(req.body).then(function(result){
                    res.status(200).json({"message":"预约成功！"});
                })
            }else{
                res.status(200).json({"message":"已预约！"});
            }
        })
            
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    },
    addMoreMeetOrder: function (req, res) {
        co(function () {
            var paramU = {};
            var openId = req.body.openId;
            var memberId = req.body.openId;
            var paramT = {};
            var params = {};
            paramU.openId = openId;
           var include = [{model:db.meetOrderModule,where:paramU,required:false}];
           paramT.include = include;
            if(req.body.speakHostId != undefined){
                params.speakHostId = req.body.speakHostId;
            }
            if(req.body.meetSubjectId != undefined){
                params.meetSubjectId = req.body.meetSubjectId;
            }
          paramT.where = params;
          db.meetScheduleModule.findAll(paramT).then(function(scheduleData){
              for(var i=0;i<scheduleData.length;i++){
                var val = scheduleData[i];
                if(val.MeetOrders.length == 0){
                    var jsonData = {};
                      
                        jsonData.id = util.getUUID();
                        jsonData.createTime = util.getNowTime();
                        jsonData.meetScheduleId = val.id;
                        jsonData.openId = openId;
                        jsonData.memberId = memberId;
                        db.meetOrderModule.create(jsonData).then(function(result){
                            console.log("添加成功！");
                        })
                }
              }
                // var orderSql = "insert into meet_order values ";
                // orderSql+="('"+util.getUUID()+"','"+req.body.memberId+"','"+req.body.openId+"','"+arr[i]+"','"+util.getNowTime()+"')";
                        
            
          })
            
            res.status(200).json({"message":"预约成功！"});
        //     orderSql=orderSql.substring(0,orderSql.lastIndexOf(","));
        //    db.sequelize.query(orderSql).then(function(result){
        //     res.status(200).json({"message":"预约成功！"});
        //    })
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    },
    delMeetByMeetScheduleId: function (req, res) {
        co(function () {
            var paramT = {};
            var paramU = {};
            if(req.query.meetScheduleId != undefined){
                paramU.meetScheduleId = req.query.meetScheduleId;
            }
            if(req.query.openId != undefined){
                paramU.openId = req.query.openId;
            }
            paramT['where'] = paramU;
            //destroy、方法就是删除记录的方法 paramT是删除条件
            db.meetOrderModule.destroy(paramT).then(function (results) {
                res.status(200).json(results);
            });
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    },
    pushOrder: function (req, res) {
        co(function () {
          var nowTime = util.getNowTime();
          var tenTime = util.getNowTenTime();
		  console.log(req.query.minStr+"==================");
          if(req.query.minStr == "2"){
              console.log(req.query.minStr+"==================");
            tenTime = util.getNowThirtyTime();
          }
		  console.log(tenTime+"==================");
		  // nowTime = "2018-04-13 12:50:00";
		  // tenTime = "2018-04-13 13:10:00";
         var params = {};
         var paramT = {};
		 paramT.attributes = ['scheduleTitle','name','startTime','endTime','meetTime','type'];
        params["$and"] = [{"startTime":{"$lte":tenTime}},{"startTime":{"$gte":nowTime}}];
         var include = [{model:db.meetOrderModule,attributes:['openId']},{model:db.meetSubjectModule,attributes:['subjectTitle']},{model:db.buildModule,attributes:['buildTitle']},{model:db.speakHostModule,attributes:['name','hospitalName']}];
         paramT.include = include;
        paramT.where = params;
         db.meetScheduleModule.findAll(paramT).then(function(result){
            res.status(200).json({"result":result});
         })
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    }
}
module.exports = meetOrderController;